//
//  RYBiaoQianView.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/20.
//  Copyright © 2021 叁拾叁. All rights reserved.
//


import UIKit

/// 列表页横向标签选择控件
class RYBiaoQianView: SYBaseView {
    
    
    public var items: [RYBiaoqianItem] = []
    
    public var lastIndex: Int = 0
    
    var defaultIndex: Int = 0
    var itemWidths: [CGFloat] = []
    var currentItemLeft: CGFloat = 0.0
    var defaultColor = UIColor(hex: 0x333333)
    var selectColor = UIColor(hex: 0x367FF6)
    var font = UIFont.systemFont(ofSize: 13)
    public var itemHeight: CGFloat = 18.0
    public var signViewWidth: CGFloat = 55.0
    public var signViewHeight: CGFloat = 2.0
    public var itemMargin: CGFloat = 10.0
    public var leftMargin: CGFloat = 15.0
    public var rightMargin: CGFloat = 15.0
    
    public var bgView: UIScrollView = {
        let v:UIScrollView = UIScrollView()
        
        return v
    }()
    
    public var signView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = UIColor.white
        v.layer.cornerRadius = 1
        
        return v
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    public func configureView(titles: [String]) {
        
            if items.count == 0 {
                initViews(titles)
            }
            
    }
    
    public func refreshItems(_ index: Int) {
        
        if lastIndex == index {
            return
        }
        lastIndex = index
        currentItemLeft = leftMargin
        for (i, value) in items.enumerated() {
            
            if i == index {
                value.titleLb.textColor = selectColor
                let x = currentItemLeft + (itemWidths[i] - signViewWidth) / 2
                if x < self.bgView.contentOffset.x || x+signViewWidth > self.bgView.contentOffset.x + self.frame.size.width {
                    let OffSetX = (x + itemWidths[i] - self.frame.size.width)
                    if OffSetX > 0 {
                        self.bgView.setContentOffset(CGPoint(x: OffSetX, y: 0), animated: true)
                    } else {
                        self.bgView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
                    }
                }
                UIView.animate(withDuration: 0.25) {
                    self.signView.frame = CGRect(x: x, y: self.frame.size.height - 2, width: self.signViewWidth, height: self.signViewHeight)
                }
            } else {
                value.titleLb.textColor = defaultColor
            }
            let w = itemWidths[i] + itemMargin
            currentItemLeft += w
        }
    }
    
    fileprivate func initViews(_ titles: [String]) {
       
        var totalItemWidth: CGFloat = leftMargin
        for (i, value) in titles.enumerated() {
            
            let tagTextSize: CGSize = value.getSize(font: font, maxSize: CGSize(width: self.frame.size.width, height: itemHeight))
            if i == defaultIndex {
                signView.frame = CGRect(x: totalItemWidth + (tagTextSize.width - signViewWidth)/2, y: self.frame.size.height - signViewHeight - 1, width: signViewWidth, height: signViewHeight)
                signView.backgroundColor = selectColor
                bgView.addSubview(signView)
            }
            
            let item: RYBiaoqianItem = RYBiaoqianItem(frame: CGRect(x: totalItemWidth, y: 0, width: tagTextSize.width, height: self.frame.size.height))
            
            itemWidths.append(tagTextSize.width)
            let w = itemMargin + tagTextSize.width
            totalItemWidth += w
            item.tag = 100 + i
            item.titleLb.text = titles[i]
            item.titleLb.textColor = defaultColor
            item.titleLb.font = font
            item.clickedClosure = {[weak self] index in
                if let weakSelf = self {
                    weakSelf.clickedClosure?(index - 100)
                    weakSelf.refreshItems(index - 100)
                }
            }
            items.append(item)
            bgView.addSubview(item)
            
        }

        bgView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
        bgView.contentSize = CGSize(width: totalItemWidth - itemMargin + rightMargin, height: self.frame.size.height)
        bgView.showsVerticalScrollIndicator = false
        bgView.showsHorizontalScrollIndicator = false
        bgView.panGestureRecognizer.delaysTouchesBegan = true // mark 解决scrollview上添加button不能滑动问题
        self.addSubview(bgView)
        
    }
    
    func resetViews(_ titles: [String], _ selectIndex: Int) {
        self.defaultIndex = selectIndex
        self.lastIndex = selectIndex
        for (i, value) in items.enumerated() {
            value.removeFromSuperview()
        }
        items.removeAll()
        initViews(titles)
    }

}


/// 列表页选择控件item
class RYBiaoqianItem: UIView {
    
    public var clickedClosure: ((Int) -> Void)?
    
    
    
    public var titleLb: UILabel = {
        let v: UILabel = UILabel()
        
        
        return v
    }()
    
    public lazy var aboveBtn: UIButton = {
        let v: UIButton = UIButton()
        v.addTarget(self, action: #selector(aboveBtnAction), for: .touchUpInside)
        return v
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    @objc func aboveBtnAction() {
        clickedClosure?(self.tag)
    }
    
    fileprivate func initViews() {
        
        
        addSubview(titleLb)
        addSubview(self.aboveBtn)
        
        titleLb.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.height.equalTo(18)
        }
        
        aboveBtn.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
}
